<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item label="类目名称">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入类目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="groupList"
      :cell-class-name="delLine"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="groupName" label="类目名称" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
          >新增</el-button>
          <el-button size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            v-if="scope.row.status === '0'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleUse(scope.row)"
          >启用</el-button>
          <el-button
            v-if="scope.row.status !== '0'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleUnUse(scope.row)"
          >禁用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级分组" prop="parentId">
              <treeselect
                v-model="form.parentId"
                :options="groupOptions"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择上级类目"
                :flat="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="类目名称" prop="groupName">
              <el-input v-model.trim="form.groupName" placeholder="请输入类目名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="排序" prop="corder">
              <el-input-number v-model.trim="form.corder" placeholder="请输入排序" :min="0" :max="9999" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import "@/assets/styles/vue-treeselect.css";
import {addGroup, deleteGroup, getGroupInfo, listGroup, updateGroup, updateStatus} from "@/api/custom/group/group";
import {delMenu} from "@/api/system/menu";

export default {
  name: "Group",
  components: {Treeselect},
  data(){
    return{
      // 查询条件
      queryParams: {
        groupName: '',
        status: ''
      },
      // 状态选项
      statusOptions: [],
      // 显示加载
      loading: false,
      // 分组列表
      groupList: [],
      // 弹窗标题
      title: '',
      // 是否显示弹窗
      open: false,
      // 弹窗表单
      form: {
        id: null,
        groupName: '',
        parentId: null,
        fullId: '',
        fullGroupName: '',
        corder: 0,
        status: '1'
      },
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: '上级分组不能为空', trigger: 'change'}
        ],
        groupName: [
          { required: true, message: "类目名称不能为空", trigger: "blur" }
        ]
      },
      // 分组树选项
      groupOptions: [],

    }
  },
  created() {
    this.getDicts("custom_status").then(response => {
      this.statusOptions = response.data;
    });
    this.handleQuery()
  },
  methods: {
    // 查询
    handleQuery() {
      this.getList()
    },
    // 查询分组列表
    getList() {
      this.loading = true;
      listGroup(this.queryParams).then(response => {
        this.groupList = this.handleTree(response.data, "id");
        this.loading = false;
      });
    },
    // 新增
    handleAdd(row) {
      this.reset()
      this.getTreeselect();
      if (row != null && row.hasOwnProperty('id')) {
        this.form.parentId = row.id;
      }
      this.open = true
      this.title = '新增分组'
    },
    // 修改分组
    handleUpdate(row) {
      this.reset()
      this.getTreeselect();
      getGroupInfo(row.id).then(response=>{
        this.form.id = response.data.id
        this.form.parentId = response.data.parentId
        this.form.groupName = response.data.groupName
        this.form.corder = response.data.corder
        this.open = true
        this.title = '修改分组'
      })
    },
    // 删除分组
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.groupName + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return deleteGroup(row.id);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function() {});
    },
    handleUse(row) {
      this.$confirm('是否确认启用名称为"' + row.groupName + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        var obj = {}
        obj.id = row.id
        obj.status = '1'
        return updateStatus(obj)
      }).then(() => {
        this.getList();
        this.msgSuccess("状态修改成功");
      }).catch(function() {});
    },
    handleUnUse(row) {
      this.$confirm('是否确认禁用名称为"' + row.groupName + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        var obj = {}
        obj.id = row.id
        obj.status = '0'
        return updateStatus(obj)
      }).then(() => {
        this.getList();
        this.msgSuccess("状态修改成功");
      }).catch(function() {});
    },
    // 表单重置
    reset() {
      this.form.id = null
      this.form.groupName = ''
      this.form.parentId = null
      this.form.fullId = ''
      this.form.fullGroupName = ''
      this.form.corder = 0
      this.form.status = '1'
      this.resetForm("form")
    },
    // 状态格式化
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 转换菜单数据结构
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.groupName,
        children: node.children,
        disabled: false
      };
    },
    // 查询分组下拉树结构
    getTreeselect() {
      listGroup().then(response => {
        this.groupOptions = [];
        const group = { id: 0, groupName: '主类目', children: [] };
        group.children = this.handleTree(response.data, "id");
        this.groupOptions.push(group);
      });
    },
    // 提交表单
    submitForm() {
      this.$refs["form"].validate(valid=>{
        if(valid) {
          if (this.form.id != undefined && this.form.id != null && this.form.id != '') {
            updateGroup(this.form).then(response=> {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            })
          } else {
            addGroup(this.form).then(response=>{
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            })
          }
        }
      })
    },
    // 关闭弹窗
    cancel() {
      this.reset()
      this.open = false
    },
    // 显示删除线
    delLine({ row, column, rowIndex, columnIndex }) {
      if (row.status === '0') return "del_line";
    },
  }
}
</script>



<style>
.del_line::after {
  content: no-open-quote;
  position: absolute;
  top: 51%;
  left: 0;
  width: 100%;
  border-bottom: 1px solid #000;
}
</style>
